/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package escom.ipn.modelo.dao;

import escom.ipn.modelo.dto.RadioClass;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
/**
 *
 * @author db2admin
 */
public class adminRadioDAO {
    
    
    public static final String SQL_RADIOS="select nombre from radio_comunicacion where asignado=0";
     
     public static final String SQL_RADIO_ID="select id_radio from radio_comunicacion where nombre=?";
     
     public static final String SQL_RADIO_NOMBRE="select nombre from radio_comunicacion where id_radio=?";
     
     public static final String SQL_RADIO_ASIGNACION="update radio_comunicacion set asignado=? where id_radio=?";
     
        public List getRadios(Connection conn) throws SQLException{
        PreparedStatement ps = null;
        ResultSet rs = null;
        List results = new ArrayList();
        ps = conn.prepareStatement(SQL_RADIOS);
        rs=ps.executeQuery();
        while(rs.next()){
            RadioClass a =new RadioClass();
            a.setNombre(rs.getString(1));
            results.add(a);
        }
        return results;            
        }
        
        public List getRadioID(RadioClass a, Connection conn) throws SQLException{
        PreparedStatement ps = null;
        ResultSet rs = null;
        List results = new ArrayList();
        ps = conn.prepareStatement(SQL_RADIO_ID);
        ps.setString(1,a.getNombre());
        rs=ps.executeQuery();
        while(rs.next()){
            RadioClass b =new RadioClass();
            b.setId(rs.getInt(1));
            results.add(b);
        }
        return results;              
        }
        
        public List getRadioNOMBRE(RadioClass a,Connection conn) throws SQLException{
        PreparedStatement ps = null;
        ResultSet rs = null;
        List results = new ArrayList();
        ps = conn.prepareStatement(SQL_RADIO_NOMBRE);
        ps.setInt(1,a.getId());
        rs=ps.executeQuery();
        while(rs.next()){
            RadioClass b =new RadioClass();
            b.setNombre(rs.getString(1));
            results.add(b);
        }
        return results;          
        }
        
        
        private void cerrar(Connection cnn) {
        if (cnn != null) {
            try {
                cnn.close();
            } catch (SQLException e) {
                //manejo del error
            }
        }
    }

    private void cerrar(PreparedStatement ps) {
        if (ps != null) {
            try {
                ps.close();
            } catch (SQLException e) {
                //manejo del error
            }
        }
    }

    private void cerrar(ResultSet rs) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                //manejo del error
            }
        }
    }

    public void asignacion(RadioClass a, Connection conn) throws SQLException {
        PreparedStatement ps = null;
        ps = conn.prepareStatement(SQL_RADIO_ASIGNACION);
        ps.setInt(1,a.getAsignado());
        ps.setInt(2,a.getId());
        ps.executeUpdate();
        cerrar(ps);
        
    }
}
